<template>
	<view class="container">
		<view class="content">
			<view class="title">
				意见反馈
			</view>
			<u--textarea v-model="value1" placeholder="请输入内容"></u--textarea>
			<view class="title">
				上传图片
			</view>
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
				:maxCount="5"></u-upload>
			<button class="btn flex-col" @click="submit">
				<text class="text_8">提交</text>
			</button>
		</view>
	</view>
</template>

<script>
	import {
		feedbackApi
	} from '@/api/api.js'
	export default {
		data() {
			return {
				content: {},
				fileList1: [],
				value1: "",
				baseUrl: ""
			};
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: "意见反馈"
			})
			this.baseUrl = uni.getStorageSync("baseUrl")
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						image: result.url,
						url: result.fullurl
					}))
					fileListLen++
				}
				console.log(this.fileList1, "<<<<<<<<<<<<<<")
			},
			uploadFilePromise(url) {
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.baseUrl + "/api/common/upload",
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data).data)
							}, 1000)
						}
					});
				})
			},
			submit() {
				let img = []
				this.fileList1.map(item => {
					img.push(item.image)
				})
				console.log(this.fileList1, img.join(","))
				feedbackApi({
					content: this.value1,
					images: img.join(",")
				}).then(res => {
					console.log(res)
					uni.$u.toast(res.data.msg)
					this.fileList1 = []
					this.value1 = ""
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: calc(100vh - var(--status-bar-height) - 88rpx);
		min-height: auto;
		background: url('../../static/image/mine/setting_bg.png') no-repeat center / cover;

		.content {
			padding: 40rpx;

			.title {
				font-size: 28rpx;
				margin: 40rpx 0 20rpx;
			}

			.btn {
				background-color: rgba(39, 85, 21, 1);
				border-radius: 24px;
				margin: 96rpx 0 20rpx 0rpx;

				.text_8 {
					overflow-wrap: break-word;
					color: rgba(255, 255, 255, 1);
					font-size: 28rpx;
					font-family: PingFangSC-Medium;
					font-weight: 500;
					text-align: center;
					white-space: nowrap;
					line-height: 92rpx;
				}
			}
		}
	}
</style>